package com.dp.temp;

import com.alibaba.fastjson.JSONObject;
import com.dp.enmu.CommonEnum;
import com.dp.io.JSONUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@RestController
@RequestMapping("/ZXJCDR")
public class zhuanxiangjianchadaoru {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 上传模板-安全检查项
     * */
    @PostMapping(value = "/company", produces = {"application/json;charset=UTF-8"})
    public JSONObject company(@RequestParam("file") MultipartFile file) {
        try {
            String msg = companyUP(file);
            if("".equals(msg)){
                return JSONUtil.SUCCESSJson(msg, CommonEnum.SUCCESS_CODE, CommonEnum.SUCCESS_MSG);
            }else {
                return JSONUtil.SUCCESSJson(msg, CommonEnum.FAILE_CODE, "上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONUtil.SUCCESSJson("上传失败", CommonEnum.FAILE_CODE, "上传失败");
        }
    }




    /**
     * 上传模板-安全检查项
     * */
    @PostMapping(value = "/item", produces = {"application/json;charset=UTF-8"})
    public JSONObject item(@RequestParam("file") MultipartFile file) {
        try {
            String msg = itemUP(file);
            if("".equals(msg)){
                return JSONUtil.SUCCESSJson(msg, CommonEnum.SUCCESS_CODE, CommonEnum.SUCCESS_MSG);
            }else {
                return JSONUtil.SUCCESSJson(msg, CommonEnum.FAILE_CODE, "上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONUtil.SUCCESSJson("上传失败", CommonEnum.FAILE_CODE, "上传失败");
        }
    }




    /**
     * 上传模板-安全检查项
     * */
    @PostMapping(value = "/score", produces = {"application/json;charset=UTF-8"})
    public JSONObject score(@RequestParam("file") MultipartFile file) {
        try {
            String msg = scoreUP(file);
            if("".equals(msg)){
                return JSONUtil.SUCCESSJson(msg, CommonEnum.SUCCESS_CODE, CommonEnum.SUCCESS_MSG);
            }else {
                return JSONUtil.SUCCESSJson(msg, CommonEnum.FAILE_CODE, "上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONUtil.SUCCESSJson("上传失败", CommonEnum.FAILE_CODE, "上传失败");
        }
    }



    /**
     * 上传模板-安全检查项
     * */
    @PostMapping(value = "/task", produces = {"application/json;charset=UTF-8"})
    public JSONObject task(@RequestParam("file") MultipartFile file) {
        try {
            String msg = taskUP(file);
            if("".equals(msg)){
                return JSONUtil.SUCCESSJson(msg, CommonEnum.SUCCESS_CODE, CommonEnum.SUCCESS_MSG);
            }else {
                return JSONUtil.SUCCESSJson(msg, CommonEnum.FAILE_CODE, "上传失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JSONUtil.SUCCESSJson("上传失败", CommonEnum.FAILE_CODE, "上传失败");
        }
    }

    public String companyUP(MultipartFile file) {
        List<Map<String, Object>> listTotal = new ArrayList<>();
        try {
            int n = 0;
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (Row row:sheet){//逐行插入数据
                if (n < 1){
                    n++;
                    continue;
                }
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);

                Map<String, Object> aaaa = new HashMap<>();
                aaaa.put("id", row.getCell(0).getStringCellValue());
                aaaa.put("task_id", row.getCell(1).getStringCellValue());
                aaaa.put("company_code", row.getCell(2).getStringCellValue());
                aaaa.put("deleted", row.getCell(3).getStringCellValue());
                aaaa.put("create_date", row.getCell(4).getStringCellValue());
                aaaa.put("create_by", row.getCell(5).getStringCellValue());
                aaaa.put("update_date", row.getCell(6).getStringCellValue());
                aaaa.put("update_by", row.getCell(7).getStringCellValue());
//                aaaa.put("bjxfbm", row.getCell(8).getStringCellValue());//这个不知道是什么数据字段，固定值不用理会
                listTotal.add(aaaa);
            }
            listTotal.forEach(aaaa -> {
                jdbcTemplate.update("insert into inspect_company (id,task_id,company_code,deleted,create_date,create_by,update_date,update_by) values (?,?,?,?,?,?,?,?)", new Object[]{aaaa.get("id"),aaaa.get("task_id"),aaaa.get("company_code"),aaaa.get("deleted"),aaaa.get("create_date"),aaaa.get("create_by"),aaaa.get("update_date"),aaaa.get("update_by")});
            });
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "新增失败";
        }
    }

    public String itemUP(MultipartFile file) {
        List<Map<String, Object>> listTotal = new ArrayList<>();
        try {
            int n = 0;
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (Row row:sheet){//逐行插入数据
                if (n < 1){
                    n++;
                    continue;
                }
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(11).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(12).setCellType(Cell.CELL_TYPE_STRING);
                Map<String, Object> aaaa = new HashMap<>();
                aaaa.put("id", row.getCell(0).getStringCellValue());
                aaaa.put("taskId", row.getCell(1).getStringCellValue());
                aaaa.put("checkName", row.getCell(2).getStringCellValue());
                aaaa.put("checkContent", row.getCell(3).getStringCellValue());
                aaaa.put("checkWay", row.getCell(4).getStringCellValue());
                aaaa.put("checkBasis", row.getCell(5).getStringCellValue());
                aaaa.put("applicable_place", row.getCell(6).getStringCellValue());
                aaaa.put("deleted", row.getCell(7).getStringCellValue());
                aaaa.put("create_by", row.getCell(8).getStringCellValue());
                aaaa.put("create_date", row.getCell(9).getStringCellValue());
                aaaa.put("update_by", row.getCell(10).getStringCellValue());
                aaaa.put("update_date", row.getCell(11).getStringCellValue());
            }
            rocketSaveItem(listTotal);
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "新增失败";
        }
    }

    public String scoreUP(MultipartFile file) {
        List<Map<String, Object>> listTotal = new ArrayList<>();
        try {
            int n = 0;
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (Row row:sheet){//逐行插入数据
                if (n < 1){
                    n++;
                    continue;
                }
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
                Map<String, Object> aaaa = new HashMap<>();
                aaaa.put("id", row.getCell(0).getStringCellValue());
                aaaa.put("checkItemId", row.getCell(1).getStringCellValue());
                aaaa.put("isReject", row.getCell(2).getStringCellValue());
                aaaa.put("deductPoint", row.getCell(3).getStringCellValue());
                aaaa.put("deductIllustrate", row.getCell(4).getStringCellValue());
                aaaa.put("ins_describe", row.getCell(5).getStringCellValue());
                aaaa.put("deleted", row.getCell(6).getStringCellValue());
                aaaa.put("create_by", row.getCell(7).getStringCellValue());
                aaaa.put("create_date", row.getCell(8).getStringCellValue());
                aaaa.put("update_by", row.getCell(9).getStringCellValue());
                aaaa.put("update_date", row.getCell(10).getStringCellValue());
            }
            rocketSaveScore(listTotal);
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "新增失败";
        }
    }

    public String taskUP(MultipartFile file) {
        List<Map<String, Object>> listTotal = new ArrayList<>();
        try {
            int n = 0;
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = workbook.getSheetAt(0);
            for (Row row:sheet){//逐行插入数据
                if (n < 1){
                    n++;
                    continue;
                }
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
                Map<String, Object> aaaa = new HashMap<>();
                aaaa.put("id", row.getCell(0).getStringCellValue());
                aaaa.put("taskName", row.getCell(1).getStringCellValue());
                aaaa.put("taskStartTime", row.getCell(2).getStringCellValue());
                aaaa.put("taskEndTime", row.getCell(3).getStringCellValue());
                aaaa.put("taskTypeName", row.getCell(4).getStringCellValue());
                aaaa.put("taskContent", row.getCell(5).getStringCellValue());
                aaaa.put("deleted", row.getCell(6).getStringCellValue());
                aaaa.put("createBy", row.getCell(7).getStringCellValue());
                aaaa.put("createDate", row.getCell(8).getStringCellValue());
                aaaa.put("updateBy", row.getCell(9).getStringCellValue());
                aaaa.put("updateDate", row.getCell(10).getStringCellValue());
            }
            rocketSavePlan(listTotal);
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "新增失败";
        }
    }




    public void rocketSavePlan(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list){
            map.put("checkType","部级检查");
            map.put("if_hide","0");
            jdbcTemplate.execute(" INSERT INTO inspect_plan (id, ins_id, ins_name, ins_type, ins_starttime, ins_endtime, ins_describe, ins_type_name, item_status, item_type, finish_state, deleted, create_date, create_by, update_date, update_by, if_hide) VALUES ('" + map.get("id") + "', '"+ map.get("taskId") +"', '"+ map.get("taskName") +"','" + map.get("checkType") + "', '" + map.get("taskStartTime") + "', '" + map.get("taskEndTime") + "', '" + map.get("taskContent") + "', '" + map.get("taskTypeName") + "', '0', '0', '1', '" + map.get("deleted") + "', '" + map.get("createDate") + "', '" + map.get("createBy") + "', '" + map.get("updateDate") + "', '" + map.get("updateBy") + "', '"+ map.get("if_hide") +"') ON DUPLICATE KEY UPDATE ins_id = '"+ map.get("taskId") +"', ins_name = '" + map.get("taskName") + "', ins_type = '" + map.get("checkType") + "', ins_starttime = '" + map.get("taskStartTime") + "',ins_endtime = '" + map.get("taskEndTime") + "', ins_describe = '" + map.get("taskContent") + "', ins_type_name = '" + map.get("taskTypeName") + "', item_status ='1', item_type = '0', finish_state = '1', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "', if_hide = '"+ map.get("if_hide") +"' , ONAME = '', OID = '', RISK_UNIT_NAME ='', RISK_UNIT_ID= '' ");

            //将所有的相同的计划id，已下发的任务都标记删除，已生成台账的信息保留
            jdbcTemplate.execute(" update from inspect_item set deleted = '1' where task_status = '0' and ins_id = '"+ map.get("taskId") +"' ");
        }
    }

    public void rocketSaveItem(List<Map<String, Object>> list) {
        Map<String, Object> itemIds = new HashMap<>();
        for (Map<String, Object> map : list){
            //将归属与同一计划id的检查项拼接在一起，然后统一将计划id的检查项字段更改过来。实现计划表中记录着绑定的检查项id
            if(!itemIds.containsKey(map.get("taskId").toString())){
                itemIds.put(map.get("taskId").toString(), map.get("checkItemId"));
            }else {
                itemIds.put(map.get("taskId").toString(), itemIds.get(map.get("taskId")).toString()+ "," + map.get("checkItemId"));
            }

            jdbcTemplate.execute(" INSERT INTO inspect_item (id, item_id, ins_id, check_num, check_content, check_way, check_basis, applicable_place, source_type, deleted, create_date, create_by, update_date, update_by) VALUES ('" + map.get("id") + "', '"+ map.get("checkItemId") +"', '"+ map.get("taskId") +"','" + map.get("checkName") + "', '" + map.get("checkContent") + "', '" + map.get("checkWay") + "', '" + map.get("checkBasis") + "', '" + map.get("applicablePlace") + "', '"+map.get("checkType")+"', '" + map.get("deleted") + "', '" + map.get("createDate") + "', '" + map.get("createBy") + "', '" + map.get("updateDate") + "', '" + map.get("updateBy") + "') ON DUPLICATE KEY UPDATE item_id = '"+ map.get("checkItemId") +"', ins_id = '" + map.get("taskId") + "', check_num = '" + map.get("checkName") + "', check_content = '" + map.get("checkContent") + "',check_way = '" + map.get("checkWay") + "', check_basis = '" + map.get("checkBasis") + "', applicable_place = '" + map.get("applicablePlace") + "', source_type = '"+map.get("checkType")+"', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "' ");
        }
        if(list.size() != 0){
            for (String key : itemIds.keySet()) {
                jdbcTemplate.execute(" update inspect_plan set item_ids = '"+ itemIds.get(key) +"' where ins_id = '"+ key +"' ");
            }
        }
    }

    public void rocketSaveScore(List<Map<String, Object>> list) {
        Map<String, Object> scoreIds = new HashMap<>();
        for (Map<String, Object> map : list){
            if(!scoreIds.containsKey(map.get("taskId").toString())){
                scoreIds.put(map.get("taskId").toString(), map.get("scoreId"));
            }else {
                scoreIds.put(map.get("taskId").toString(), scoreIds.get(map.get("taskId")).toString() +","+map.get("scoreId") );
            }

            jdbcTemplate.execute(" INSERT INTO inspect_score (id, score_id, item_id, ins_id, score_name, is_reject, deduct_point, deduct_illustrate, source_type, deleted, create_date, create_by, update_date, update_by) VALUES ('"+ map.get("id") +"', '"+ map.get("scoreId") +"', '"+ map.get("checkItemId") +"', '"+ map.get("taskId") +"', '政府下发评分细则项', '"+ map.get("isReject") +"', '"+ map.get("deductPoint") +"', '"+ map.get("deductIllustrate") +"', '"+map.get("checkType")+"', '"+ map.get("deleted") +"', '"+ map.get("createDate") +"', '"+ map.get("createBy") +"', '"+ map.get("updateDate") +"', '"+ map.get("updateBy") +"') ON DUPLICATE KEY UPDATE score_id = '"+ map.get("scoreId") +"', item_id = '" + map.get("checkItemId") + "', ins_id = '" + map.get("taskId") + "', score_name = '政府下发评分细则项', is_reject = '" + map.get("isReject") + "', deduct_point = '" + map.get("deductPoint") + "', deduct_illustrate = '" + map.get("deductIllustrate") + "', source_type = '"+map.get("checkType") +"', deleted = '" + map.get("deleted") + "', create_date = '" + map.get("createDate") + "', create_by = '" + map.get("createBy") + "', update_date = '" + map.get("updateDate") + "', update_by = '" + map.get("updateBy") + "' ");
        }
        if(list.size() != 0){
            for (String key : scoreIds.keySet()) {
                jdbcTemplate.execute(" update inspect_plan set score_ids = '"+ scoreIds.get(key) +"' where ins_id = '"+ key +"' ");
            }
        }
    }



}
